// 方式一，编译时创建单例
class Singleton1 {
    //1.构造器私有化，外部不能new
    private constructor(){}
    //2.本类内部创建对象实例化
    private static instance : Singleton1 = new Singleton1()
    //3.提供一个静态的唯一访问方法，返回实例对象
    public static getInstance () : Singleton1{
        return this.instance
    }
}

//方式二,运行时创建实例
class Singleton2{
    //私有构造器
    private constructor(){}
    //声明唯一私有实例但不初始化
    private static instance : Singleton2;
    //判断唯一实例是否已经被创建，若为 undefine 则创建实例 
    public static getInstance ():Singleton2{
        this.instance = this.instance ?? new Singleton2()
        return this.instance
    }
}

const SingletonTest = ():void=>{
    console.log(Singleton1.getInstance());
    console.log(Singleton2.getInstance());
}

SingletonTest()


